/*
 * Main.c
 *
 *  Created on: 16 juli 2021
 *      Author: Daniel Mårtensson
 */

#include <stdio.h>

/* Include Open SAE J1939 */
#include "Open_SAE_J1939/Open_SAE_J1939.h"

int main() {

	/* Create our J1939 structure with two ECU */
	J1939 j1939_1 = {0};
	J1939 j1939_2 = {0};

	/* Important to sent all non-address to 0xFF - Else we cannot use ECU address 0x0 */
	uint8_t i;
	for(i = 0; i < 255; i++){
		j1939_1.other_ECU_address[i] = 0xFF;
		j1939_2.other_ECU_address[i] = 0xFF;
	}

	/* Set the ECU address - Notice that they are the same address! */
	j1939_1.information_this_ECU.this_ECU_address = 0x80;												/* From 0 to 253 because 254 = error address and 255 = broadcast address */
	j1939_2.information_this_ECU.this_ECU_address = 0x80;

	/* Set NAME for ECU 1 */
	j1939_1.information_this_ECU.this_name.identity_number = 100; 										/* From 0 to 2097151 */
	j1939_1.information_this_ECU.this_name.manufacturer_code = 300; 									/* From 0 to 2047 */
	j1939_1.information_this_ECU.this_name.function_instance = 10; 										/* From 0 to 31 */
	j1939_1.information_this_ECU.this_name.ECU_instance = 2; 											/* From 0 to 7 */
	j1939_1.information_this_ECU.this_name.function = FUNCTION_VDC_MODULE;								/* From 0 to 255 */
	j1939_1.information_this_ECU.this_name.vehicle_system = 100;										/* From 0 to 127 */
	j1939_1.information_this_ECU.this_name.arbitrary_address_capable = 0;								/* From 0 to 1 */
	j1939_1.information_this_ECU.this_name.industry_group = INDUSTRY_GROUP_CONSTRUCTION;				/* From 0 to 7 */
	j1939_1.information_this_ECU.this_name.vehicle_system_instance = 10;								/* From 0 to 15 */

	/* Set NAME for ECU 2 */
	j1939_2.information_this_ECU.this_name.identity_number = 1000; 										/* From 0 to 2097151 */
	j1939_2.information_this_ECU.this_name.manufacturer_code = 400; 									/* From 0 to 2047 */
	j1939_2.information_this_ECU.this_name.function_instance = 20; 										/* From 0 to 31 */
	j1939_2.information_this_ECU.this_name.ECU_instance = 1; 											/* From 0 to 7 */
	j1939_2.information_this_ECU.this_name.function = FUNCTION_AUXILIARY_VALVES_CONTROL;				/* From 0 to 255 */
	j1939_2.information_this_ECU.this_name.vehicle_system = 50;											/* From 0 to 127 */
	j1939_2.information_this_ECU.this_name.arbitrary_address_capable = 0;								/* From 0 to 1 */
	j1939_2.information_this_ECU.this_name.industry_group = INDUSTRY_GROUP_AGRICULTURAL_AND_FORESTRY;	/* From 0 to 7 */
	j1939_2.information_this_ECU.this_name.vehicle_system_instance = 15;								/* From 0 to 15 */

	/* Broadcast NAME from ECU 1 to all ECU */
	SAE_J1939_Response_Request_Address_Claimed(&j1939_1);

	/* Listen for NAME request for ECU 2 from ECU 1 - What going to happen: ECU 2 is going to send Address Not Claimed to all ECU because it's conflicts with the ECU 1 address */
	Open_SAE_J1939_Listen_For_Messages(&j1939_2);

	/* To see that Address Not Claimed, ECU 1 need to read that message */
	Open_SAE_J1939_Listen_For_Messages(&j1939_1);

	/* Print information */
	printf("How many external ECU are connected according to ECU 1? %i\n", j1939_1.number_of_other_ECU);
	printf("How many external ECU are connected according to ECU 2? %i\n", j1939_2.number_of_other_ECU);

	/* Print information about who cannot claim their own address */
	printf("How many ECU cannot claim their address according to ECU 1? %i\n", j1939_1.number_of_cannot_claim_address);
	printf("How many ECU cannot claim their address according to ECU 2? %i\n", j1939_2.number_of_cannot_claim_address);

	return 0;
}
